package ldh.msg.ui;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import ldh.msg.MsgManager;
import ldh.msg.socket.MsgUDPManager;

public class MsgTrayManager {

	TrayIcon trayIcon;
	private static MsgTrayManager manager = null;
	private static boolean isTray = false;
	
	private MsgTrayManager() throws Exception {
		initialize();
	}
	public static MsgTrayManager getInstance() throws Exception
	{
		if (manager == null) {
			synchronized (MsgTrayManager.class) {
				manager = new MsgTrayManager();
			}
		}
		return manager;
	}
	
	private void initialize() throws Exception {
		if (SystemTray.isSupported()) {
			isTray = true;
		    SystemTray tray = SystemTray.getSystemTray();
		    Image image = Toolkit.getDefaultToolkit().getImage("conf/trayicon.png");
		    MouseListener mouseListener = new MouseListener() {
		                
		        public void mouseClicked(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse clicked! getClickCount " + e.getClickCount());
		            System.out.println("Tray Icon - Mouse clicked! getSource " + e.getSource());
		            System.out.println("Tray Icon - Mouse clicked! getButton " + e.getButton());
		            try {
		            	if (e.getButton() == 1) {
			            	if (MessengerUI.getInstance().getMainFrame().isVisible()) {
			            		MessengerUI.getInstance().setVisibleMainFrame(false);
			            	} else {
			            		MessengerUI.getInstance().setVisibleMainFrame(true);
			            	}
		            	}
		    		} catch (Exception ex) {
		    			ex.printStackTrace();
		    		}
		            
		        }
	
		        public void mouseEntered(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse entered!");                 
		        }
	
		        public void mouseExited(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse exited!");                 
		        }
	
		        public void mousePressed(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse pressed!");                 
		        }
	
		        public void mouseReleased(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse released!");                 
		        }
		    };
	
		    ActionListener exitListener = new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		            System.out.println("Exiting...");
		            try {
		            	MsgUDPManager.getInstance().closingBroadCastNetWork();
		            } catch (Exception ex) {
		            	ex.printStackTrace();
		            }
		            disposeTray();
		            System.exit(0);
		        }
		    };
		            
		    PopupMenu popup = new PopupMenu();
		    MenuItem defaultItem = new MenuItem("Exit");
		    defaultItem.addActionListener(exitListener);
		    popup.add(defaultItem);
	
		    trayIcon = new TrayIcon(image, "Java MessagePop", popup);
	
		    ActionListener actionListener = new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		            trayIcon.displayMessage("Action Event", 
		                "An Action Event Has Been Performed!",
		                TrayIcon.MessageType.INFO);
		        }
		    };
		            
		    trayIcon.setImageAutoSize(true);
		    trayIcon.addActionListener(actionListener);
		    trayIcon.addMouseListener(mouseListener);
	
		    try {
		        tray.add(trayIcon);
		    } catch (AWTException e) {
		        System.err.println("TrayIcon could not be added.");
		    }
	
		} else {
	
		    //  System Tray is not supported
	
		}
	}
	
	public void displayTrayNewMsg(int cnt) {
		if (trayIcon != null) {
			System.out.println(cnt + " New Message");
			trayIcon.displayMessage("You've got message", 
	                cnt + " New Message",
	                TrayIcon.MessageType.INFO);
		}
	}
	
	public void disposeTray() {
		if (trayIcon != null) {
			trayIcon = null;
		}
			
	}
}
